// 厦门大学计算机专业 | 前华为工程师
// 专注《零基础学编程系列》  http://lblbc.cn/blog
// 包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
// 公众号：蓝不蓝编程

import SwiftUI
import SDWebImageSwiftUI

struct OrderDetailView: View {
    var orderInfo: OrderInfo?
    var orderId: String
    @StateObject private var viewModel = OrderDetailViewModel()
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    
    var body: some View {
        VStack{
            if let orderInfo = viewModel.orderInfo {
                buildOrderItemView(orderInfo: orderInfo)
                buildOrderExtraView(orderInfo: orderInfo)
                Spacer()
                buildBottomView(orderInfo: orderInfo)
            }
        }
        .frame(maxWidth: .infinity,maxHeight: .infinity, alignment: .top)
        .background(Color(hex: 0xF4F4F4))
        .onAppear(perform: {
            viewModel.queryOrder(orderId: orderId)
        })
    }
    
    private func buildOrderItemView(orderInfo: OrderInfo) -> some View{
        return VStack{
            VStack{                
                buildOrderListView(list: orderInfo.list)
                
                Divider()
                HStack{
                    Text("商品总金额")
                    Spacer()
                    //                    Text("￥\(orderDetail.price*orderDetail.quantity/100)")
                }.padding(EdgeInsets.init(top: 5, leading: 0, bottom: 0, trailing: 0))
                //                Text("实付￥\(orderDetail.price*orderDetail.quantity/100)").font(.title3)
            }.padding(EdgeInsets.init(top: 10, leading: 10, bottom: 10, trailing: 10))
                .frame(maxWidth: .infinity,alignment: .leading)
                .background(.white)
                .clipShape(RoundedRectangle(cornerRadius: 5))
                .padding(EdgeInsets.init(top: 10, leading: 10, bottom: 0, trailing: 10))
        }
    }
    
    
    private func buildOrderExtraView(orderInfo: OrderInfo) -> some View{
        VStack(alignment: .leading){
            Text("取餐信息")
            HStack{
                Text("取餐号").foregroundColor(Color(hex: 0x97999E))
                Text("10011").foregroundColor(Color(hex: 0x595D63)).font(.system(size:14))
            }.padding(EdgeInsets.init(top: 5, leading: 0, bottom: 0, trailing: 0))
            HStack{
                Text("下单时间").foregroundColor(Color(hex: 0x97999E))
                
            }.padding(EdgeInsets.init(top: 5, leading: 0, bottom: 0, trailing: 0))
        }.padding(EdgeInsets.init(top: 10, leading: 10, bottom: 10, trailing: 10))
            .frame(maxWidth: .infinity,alignment: .leading)
            .background(.white)
            .clipShape(RoundedRectangle(cornerRadius: 5))
            .padding(EdgeInsets.init(top: 10, leading: 10, bottom: 0, trailing: 10))
    }
    
    private func buildBottomView(orderInfo: OrderInfo) -> some View{
        return VStack{
            Divider()
            HStack{
                Spacer()
                Text("删除订单").padding(EdgeInsets.init(top: 5, leading: 10, bottom: 5, trailing: 10))
                    .background(RoundedRectangle(cornerRadius: 50).strokeBorder(Color.gray,lineWidth: 1))
                    .padding()
                    .foregroundColor(Color(hex: 0x141414))
                    .onTapGesture(perform: {
                        viewModel.deleteOrder(orderId: orderInfo.orderId){ isSuccess,msg in
                            goBack()
                        }
                    })
                }
        }.background(.white)
    }
    
    private func buildOrderListView(list: [OrderDetail]) -> some View{
        return VStack{
            ForEach(list.indices , id: \.self){ j in
                let orderDetail = list[j]
                HStack{
                    WebImage(url: URL(string: orderDetail.squarePic ?? ""))
                        .placeholder{Color.gray}
                        .resizable()
                        .scaledToFit()
                        .frame(width: 50, height: 50)
                    VStack(alignment: .leading){
                        Text(orderDetail.name).padding(EdgeInsets.init(top: 0, leading: 0, bottom: 5, trailing: 0))
                    }
                    VStack{
                        Text("￥\(orderDetail.price/100)")
                        Text("×\(orderDetail.quantity)")
                    }
                }
            }
        }
    }
    
    private func goBack(){
        self.presentationMode.wrappedValue.dismiss()
    }
}


//struct OrderDetailView_Previews: PreviewProvider {
//    static var previews: some View {
//        OrderDetailView()
//    }
//}
